package cj.web.yc.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.login.controllers.BaseController;
import cj.web.yc.constants.OrderSourceEnum;
import cj.web.yc.constants.PayStatusEnum;
import cj.web.yc.constants.PayWayEnum;
import cj.web.yc.domain.VideoOrder;
import cj.web.yc.service.VideoOrderService;

@Path("/videoOrder")
public class VideoOrderController extends BaseController{

	private static Logger logger = Logger.getLogger(VideoOrderController.class.getName());
	
	@Autowired
	private VideoOrderService videoOrderService;
	
	@Get("show")
	public String show(Model model,VideoOrder videoOrder) {
		model.add("payWayEnum",EnumSet.allOf(PayWayEnum.class));
		model.add("payStatusEnum",EnumSet.allOf(PayStatusEnum.class));
		model.add("orderSourceEnum",EnumSet.allOf(OrderSourceEnum.class));
		return "/views/yc/videoOrderList.vm";
	}

	@Post("getList")
	public String queryList(VideoOrder videoOrder) {
	
		//当前页  
		videoOrder.setStart((videoOrder.getPage()-1)*videoOrder.getRows());
		videoOrder.setLength(videoOrder.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = videoOrderService.query(videoOrder);
		List<VideoOrder> objs = (List<VideoOrder>) rep.get("videoOrderlis");
		
		for(VideoOrder obj:objs){
			obj.setPayWayName(PayWayEnum.getName(obj.getPayWay()));
			obj.setStatusName(PayStatusEnum.getName(obj.getStatus()));
		}
		
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(){
		return "/views/yc/videoOrderDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,VideoOrder videoOrder,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		videoOrder.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		videoOrder.setCreateAt(DateUtils.getNowTime());
		videoOrder.setCreateUser(returnSessionObj(request).getAccount());
		videoOrder.setUpdateAt(DateUtils.getNowTime());
		videoOrder.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			videoOrderService.create(videoOrder);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		VideoOrder videoOrder = videoOrderService.queryById(id);

		model.add("videoOrder", videoOrder);
		return "/views/yc/videoOrderEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,VideoOrder videoOrder,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		videoOrder.setUpdateAt(DateUtils.getNowTime());
		videoOrder.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			videoOrderService.update(videoOrder);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			videoOrderService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}
